﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using Admin.UI;
using Power.DAL;
using Power.Model;

namespace Power
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public string username
        {
            get;
            set;
        }

        public string usertype
        {
            get;
            set;
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Login win = new Login();
            if (win.ShowDialog() != true)
            {
                Application.Current.Shutdown();
            }
            else
            {
                username = win.ruser;

                int utype = 3;
                utype = new UserDal().GetNametype(username);
                if (utype == 3)
                {
                    btnfnrp.Visibility = Visibility.Hidden;
                    btnlscus.Visibility = Visibility.Hidden;
                    listthis.Visibility = Visibility.Hidden;
                    finderr.Visibility = Visibility.Hidden;

                }

                if (utype == 4)
                {
                    btnAdd.Visibility = Visibility.Hidden;
                    btnEdit.Visibility = Visibility.Hidden;
                    btnMin.Visibility = Visibility.Hidden;
                    btnad.Visibility = Visibility.Hidden;
                    btned.Visibility = Visibility.Hidden;
                    btnrp.Visibility = Visibility.Hidden;
                    btnlsrp.Visibility = Visibility.Hidden;
                }
            }

        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            EditUser editwin = new EditUser();
            editwin.USername = username;
            editwin.ShowDialog();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string texttemp = Findbox.Text;

            List<Customer> cuslist = new List<Customer>();

            Customer[] custa = new CustomerDAL().ListToFind(texttemp);

            foreach (Customer cusone in custa)
            {
                Customer p = new Customer();
                p.Id = cusone.Id;
                p.CusName = cusone.CusName;
                p.Device = cusone.Device;
                p.Phone = cusone.Phone;
                p.Address = cusone.Address;
                p.Charger = cusone.Charger;
                p.CID = cusone.CID;
                cuslist.Add(p);
            }
            dg1.ItemsSource = cuslist;

        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (dg1.SelectedItem == null)
            {
                MessageBox.Show("请选择");
                return;
            }
            Customer temp = (Customer)dg1.SelectedItem;
            EditCus editwin = new EditCus();
            editwin.IDtemp = temp.Id;
            editwin.CID = temp.CID;
            editwin.CUSname = temp.CusName;
            editwin.Address = temp.Address;
            editwin.Phone = temp.Phone;
            editwin.Device = temp.Device;
            editwin.ShowDialog();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            NewCus newwin = new NewCus();
            newwin.ShowDialog();
        }

        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            if (dg1.SelectedItem == null)
            {
                MessageBox.Show("请选择");
                return;
            }

            Customer temp = (Customer)dg1.SelectedItem;
            new CustomerDAL().CusDel(temp.CusName);
            MessageBox.Show("删除成功");
        }

        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            Monitorc[] mons = new MonitorDAL().ToFindError();

            List<string> listString = new List<string>();

            foreach (Monitorc monone in mons)
            {
                if (!listString.Contains(monone.Device))
                    listString.Add(monone.Device);
            }

            List<Customer> cuslist = new List<Customer>();
            foreach (string devstr in listString)
            {
                Customer p = new Customer();
                Customer cusone = new Customer();
                cusone = new CustomerDAL().DeToFind(devstr);

                p.Id = cusone.Id;
                p.CusName = cusone.CusName;
                p.Device = cusone.Device;
                p.Phone = cusone.Phone;
                p.Address = cusone.Address;
                p.Charger = cusone.Charger;
                p.CID = cusone.CID;
                cuslist.Add(p);
            }

            dg1.ItemsSource = cuslist;

        }

        private void btnRepr_Click(object sender, RoutedEventArgs e)
        {
            if (dg1.SelectedItem == null)
            {
                MessageBox.Show("请选择");
                return;
            }
            Customer temp = (Customer)dg1.SelectedItem;
            ReportList rewin = new ReportList();
            rewin.username = temp.CusName;
            rewin.device = temp.Device;
            rewin.ShowDialog();
        }

        private void btnPrere_Click(object sender, RoutedEventArgs e)
        {
            if (dg1.SelectedItem == null)
            {
                MessageBox.Show("请选择");
                return;
            }
            Customer temp = (Customer)dg1.SelectedItem;
            PreReport rewin = new PreReport();
            rewin.username = temp.CusName;
            rewin.device = temp.Device;
            rewin.ShowDialog();
        }

        private void btnAftre_Click(object sender, RoutedEventArgs e)
        {
            if (dg1.SelectedItem == null)
            {
                MessageBox.Show("请选择");
                return;
            }
            Customer temp = (Customer)dg1.SelectedItem;
            Report rewin = new Report();
            rewin.username = temp.CusName;
            rewin.device = temp.Device;
            rewin.ShowDialog();
        }

        private void Mon_Click(object sender, RoutedEventArgs e)
        {
            if (dg1.SelectedItem == null)
            {
                MessageBox.Show("请选择");
                return;
            }

            Customer temp = (Customer)dg1.SelectedItem;
            ListTemp lewin = new ListTemp();
            lewin.device = temp.Device;
            lewin.username = temp.CusName;
            lewin.ShowDialog();
        }

        private void btnRPDS_Click(object sender, RoutedEventArgs e)
        {

            List<Customer> cuslist = new List<Customer>();

            Customer[] custa = new CustomerDAL().ListToRP();

            foreach (Customer cusone in custa)
            {
                Customer p = new Customer();
                p.Id = cusone.Id;
                p.CusName = cusone.CusName;
                p.Device = cusone.Device;
                p.Phone = cusone.Phone;
                p.Address = cusone.Address;
                p.Charger = cusone.Charger;
                p.CID = cusone.CID;
                cuslist.Add(p);
            }
            dg1.ItemsSource = cuslist;

        }

        private void MenuItem_Click_3(object sender, RoutedEventArgs e)
        {
            About newab = new About();
            newab.ShowDialog();
        }
    }
}